# -*- coding: UTF-8 -*-
# __author__ = 'Sengo'
from PIL import Image, ImageFilter
import numpy as np


def undo_normalise(im):
    return (1/(np.exp(-im) + 1) * 257 - 1).astype("uint8")


def low_pass_filter(img, sigma):
    img = undo_normalise(img)
    re = Image.fromarray(img)
    re = re.filter(ImageFilter.GaussianBlur(radius=sigma))  # Image
    return np.asarray(re)


def high_pass_filter(img, sigma):
    _img = img.copy()
    low_pass = low_pass_filter(img, sigma)
    return _img - low_pass


def hybrid_img(hig, low):
    return hig + low


if __name__ == '__main__':
    from settings import BASE_DIR
    from align_image_code import align_images
    import matplotlib.pyplot as plt
    from base_service import *
    #
    im1 = plt.imread(BASE_DIR + '/img/man.jpg')
    print im1.shape
    #
    im2 = plt.imread(BASE_DIR + '/img/cat.jpg')

    re2, re1 = align_images(im2, im1)
    hig = high_pass_filter(re2, 5)
    low = low_pass_filter(re1, 5)
    re = hybrid_img(hig, low)
    plt.imshow(re)
    plt.show()